<?php
/**
 * (actions)
 */
namespace App\Admin\Actions\File\Operation;

use Encore\Admin\Admin;
use Encore\Admin\Grid\Tools\AbstractTool;
class ParseFile extends AbstractTool
{
    /**
     * Render Export button.
     *
     * @return string
     */
    public function render()
    {
        $token = csrf_token();
        $script = <<<SCRIPT
$('.parse-file-action').unbind('click').click(function(e) {
    e.preventDefault();
    var action = $(this).data('action');
    swal({
        title: "确定解析？",
        text : "确定解析文件吗？",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "确认",
        showLoaderOnConfirm: true,
        cancelButtonText: "取消",
        preConfirm: function() {
            return new Promise(function(resolve) {
                $.ajax({
                    method: 'post',
                    url: '/xqc/file/operation-file-ajax',
                    data: {
                        _token:"{$token}",
                        action:action
                    },
                    beforeSend: function(){
                        $('#loading-tasks-results').text('解析中');
                        $(this).attr('disabled',true);
                    },
                    success: function (data) {
                        resolve(data);
                    }
                });
            });
        }
    }).then(function(result) {
        var data = result.value;
        if (typeof data === 'object') {
            $('#loading-tasks-results').text('解析文件');
            $(this).removeAttr('disabled');
            if (data.status) {
                $.pjax.reload('#pjax-container');
                toastr.success('操作成功 !');
            } else {
                swal(data.message, '', 'error');
            }
        }
    });
});
SCRIPT;
        Admin::script($script);
        return <<<EOT
<div class="btn-group">
    <button class="btn btn-sm btn-warning parse-file-action" data-action="parseFile"><i class="fa fa-retweet"></i>&nbsp;<span id="loading-tasks-results">解析文件</span></button>
</div>
EOT;
    }
}
